#!/usr/bin/perl -w

BEGIN {
	if(exists($ENV{TRUECL_LIBS})) { push @INC,$ENV{TRUECL_LIBS}; }
	if(exists($ENV{TRUECL_CODE})) { push @INC,"$ENV{TRUECL_CODE}/lib"; }
}

use cldb;
use Getopt::Long;
use File::Basename;
use Data::Dumper;

$_PRG=basename($0);

sub usage {
	print STDERR "
Usage: $_PRG [--dbfile D] [--maxlen N] [--schemas]

";
	exit(1);
}

$ARG_dbfile="$::ENV{TRUECL_CFG}/config.db";
$ARG_schemas=0;
$ARG_maxlen=50;
$ARG_maxcount=10;

if(!GetOptions(
	"schemas"	=> \$ARG_schemas,
	"maxlen=i"	=> \$ARG_maxlen,
	"maxcount=i"	=> \$ARG_maxcount,
	"dbfile=s"	=> \$ARG_dbfile,
)) {
	usage;
}

if(! -f $ARG_dbfile) {
	print STDERR "Error: Database '$ARG_dbfile' file does not exist!\n";
	exit 1;
}

$DBH = new cldb($ARG_dbfile);
if(!defined($DBH)) {
	($rc,$str)=cldb::error;
	print STDERR "Error: $str [rc=$rc]\n";
	exit 1;
}

$aref=$DBH->get_rows("SELECT name FROM sqlite_master WHERE type = 'table';");
for $cref (@$aref) {
	print "\n";
	my $ARG_table=$cref->{name};

	#########################################################################
	# First thing to do is to get a list of tables that this database 	#
	# currently contains so we can dump them.				#
	#########################################################################

	if(!$DBH->exists("table",$ARG_table)) {
		print STDERR "Error: Table \"$ARG_table\" does not exist.\n";
		next;
	}

	if($ARG_schemas) {
		$txt=$DBH->get_object_schema("table",$ARG_table);
		if(defined($txt)) {
			print "$txt\n";
		}
	}

	if(!defined($ARG_clause)) {
		$cmd="SELECT * FROM $ARG_table;";
	} else {
		$cmd="SELECT * FROM $ARG_table $ARG_clause;";
	}

	$aref=$DBH->get_rows($cmd);
	$cc=0;
	if(!defined($aref)) {
		($cc,$rc)=$DBH->error;
		print "Error attempting to list table\n$rc\n";
		next;
	}

	@R=@$aref;
	next if (!@R);

	#########################################################################
	# Default column widths to display determined by the name of column.	#
	#########################################################################

	@widths=();
	@values=();

	$i=0;
	for $ckey (keys(%{$R[0]})) {
		$widths[$i]=length($ckey);
		$values[$i]=0;
		$i++;
	}

	#########################################################################
	# Widen the column widths to cope with data if necessary.		#
	#########################################################################
	for $aa (@R) {
		$i=0;
		for $ckey (keys(%$aa)) {
			$l=0;
			$l=length($aa->{$ckey}) if defined($aa->{$ckey});
			$values[$i]++ if $l;
			$widths[$i]=0 if !defined($widths[$i]);
			$widths[$i]=$l if $l > $widths[$i];
			$widths[$i]=$ARG_maxlen if $widths[$i]>$ARG_maxlen;
			$i++;
		}
	}

	#########################################################################
	# If a column has no values, then do not print it.			#
	#########################################################################

	$i=0;
	$r="";
	if($ARG_schemas==0) {
		print "TABLE: $ARG_table\n";
	}
	for $ckey (keys(%{$R[0]})) {
		if($values[$i]) {
			$r.=sprintf("%-*s  ",$widths[$i],$ckey);
		}
		$i++;
	}
	print "$r\n";

	$i=0; $r="";
	for $ckey (keys(%{$R[0]})) {
		if($values[$i]) {
			$r.="=" x $widths[$i];
			$r.="  ";
		}
		$i++;
	}
	print "$r\n";

	#########################################################################
	# Now dump out all records.						#
	#########################################################################

	$count=0;
	for $aa (@R) {
		$i=0;
		$r="";
		for $ckey (keys(%$aa)) {
			if($values[$i]) {
				if(defined($aa->{$ckey})) {
					$r.=sprintf("%-*s  ",$widths[$i],$aa->{$ckey});
				} else {
					$r.="  "." " x $widths[$i];
				}
			}
			$i++;
		}
		print "$r\n";
		$count++;
		last if $count >=$ARG_maxcount;
	}
	print "\n";
	print scalar(@R)." Record" . (scalar(@R)==1 ? "" : "s") . " found.\n";
}

exit 0;
